import axios from 'axios'
import { Toast } from 'fawkes-mobile-lib'
import { requestEncrypt, responseEncrypt } from '@/config'
import { sign, cMsg } from '@/config/request'
import { encrypt, decrypt } from '@/utils/request/encrypt'
import { verifyRequest, verifyResponse } from './verify'
import { getSign } from '@/utils/request/sign'
import storage from '@/utils/storage'
import router from '@/router'
import { verifyMonitorUrl } from '@/utils/monitor'
import getFawkesBiz from './getFawkesBiz'
//检查配置列表中是否包含当前url
const verify = function (config, arr) {
  return arr.find((item) => {
    return item == config.url
  })
}

// 创建 axios 实例
const instance = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL, // api base_url
  timeout: 10000, // 请求超时时间
  withCredentials: true,
})

instance.interceptors.request.use(
  (config) => {
    config.headers['Fawkes-Biz'] = getFawkesBiz(config) //接口统一携带'Fawkes-Biz'，用于处理接口的数据权限

    verify(config, sign)
      ? (config.params = getSign(config.params))
      : (config.headers['Fawkes-Auth'] = `${storage.get('access_token')}`)

    !!requestEncrypt && verifyRequest(config) && (config = encrypt(config))

    config = verifyMonitorUrl(config)

    return config
  },
  (error) => Promise.reject(error)
)

instance.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    !!responseEncrypt && verifyResponse(response) && (response = decrypt(response))

    if (response.config.responseType == 'blob') {
      return response
    }

    let res = response.data

    if (verify(response.config, cMsg)) {
      return res
    }
    if (res.code && 8000000 !== res.code && res.status && 200 !== res.status) {
      Toast({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000,
      })
      return Promise.reject(response)
    }
    // if the custom code is not 20000, it is judged as an error.
    return res
  },
  (error) => {
    // let message = ''
    if (error.response) {
      switch (error.response.status) {
        case 400:
          Toast({
            message: error.response.data,
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 401:
          switch (error.response.data.code) {
            case -8000140:
            case -8000170:
              {
                Toast({
                  message: error.response.data.message,
                  type: 'error',
                  duration: 5 * 1000,
                })

                router.push({ path: '/login' }).catch((err) => err)
              }
              break
            case -8000160:
              {
                Toast({
                  message: '无证书提示',
                  type: 'error',
                  duration: 5 * 1000,
                })
                // conformHandler('无证书提示', error.response.data.message, action => {
                //   router.push({ path: '/uploadPermission' })
                // })
              }
              break
            default:
              Toast({
                message: error.response.data.message,
                type: 'error',
                duration: 5 * 1000,
              })
          }
          break
        case 403:
          Toast({
            message: '拒绝访问',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 404:
          Toast({
            message: '请求错误,未找到该资源',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 405:
          Toast({
            message: '请求方法未允许',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 408:
          Toast({
            message: '请求超时',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 500:
          Toast({
            message: '服务器端出错',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 501:
          Toast({
            message: '网络未实现',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 502:
          Toast({
            message: '网络错误',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 503:
          Toast({
            message: '服务不可用',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 504:
          Toast({
            message: '网络超时',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        case 505:
          Toast({
            message: 'http版本不支持该请求',
            type: 'error',
            duration: 5 * 1000,
          })
          break
        default:
          Toast({
            message: `连接错误${error.response.status}`,
            type: 'error',
            duration: 5 * 1000,
          })
      }
      if (error.response.config && verify(error.response.config, cMsg)) {
        // message = null
      }
    } else {
      Toast({
        message: '网络异常，请检查网络！',
        type: 'error',
        duration: 3 * 1000,
      })
    }
    // message && resetMessage.error(message)
    return Promise.reject(error)
  }
)

export default instance
